package com.ejoysoft.etable.system;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Vector;

import com.ejoysoft.common.DbConnect;
import com.ejoysoft.common.Format;
import com.ejoysoft.common.Globa;
import com.ejoysoft.common.UID;
import com.ejoysoft.etable.system.Unit.UnitCompare;

public class TradeClass {

	private Globa globa;
    private DbConnect db;

    //���췽��
    public TradeClass() {
    }

    //���췽��
    public TradeClass(Globa globa, boolean b) {
        this.globa = globa;
        db = globa.db;
        if (b) globa.setDynamicProperty(this);
    }

    static String strTableName = "t_sy_tradeclass";
    
  //���
    public boolean add() {
        String strSql = "";
        try {
            strId = UID.getID();
            strSql = "INSERT INTO " + strTableName + " (strId, strCode, strName, strParentId, strCreator, " +
                    "dCreatDate,strRemark) VALUES (?,?,?,?,?,?,?) ";
            db.prepareStatement(strSql);
            db.setString(1, strId);
            db.setString(2, strCode);
            db.setString(3, strName);
            db.setString(4, strParentId);
            db.setString(5, globa.loginName);
            db.setString(6, com.ejoysoft.common.Format.getDateTime());
          
            db.setString(7, strRemark);
           
            if (db.executeUpdate() > 0) {
                Globa.logger0("�����ҵ������Ϣ", globa.loginName, globa.loginIp, strSql, "��ҵ����ά��", globa.userSession.getStrDepart());
                return true;
            } else
                return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //ɾ��
    public boolean delete(String where) {
        try {
            String sql = "DELETE FROM " + strTableName + "  ".concat(where);
            db.executeUpdate(sql);
            Globa.logger0("ɾ����ҵ������Ϣ", globa.loginName, globa.loginIp, sql, "��ҵ����ά��", globa.userSession.getStrDepart());
            return true;
        } catch (Exception ee) {
            ee.printStackTrace();
            return false;
        }
    }

    //�޸�
    public boolean update(String tStrId) {
        try {

            String strSql = "UPDATE  " + strTableName + "  SET strCode = ?, strName = ?, strParentId = ?, " +
            		"strCreator =?,dCreatDate=?,strRemark=?  WHERE strId = ? ";
            db.prepareStatement(strSql);
            db.setString(1, strCode);
            db.setString(2, strName);
            db.setString(3, strParentId);
            db.setString(4, globa.loginName);
            db.setString(5, com.ejoysoft.common.Format.getDateTime());
           
            db.setString(6, strRemark);
           
            db.setString(7, tStrId);
            db.executeUpdate();
            Globa.logger0("�޸���ҵ������Ϣ", globa.loginName, globa.loginIp, strSql, "��ҵ����ά��", globa.userSession.getStrDepart());
            return true;
        } catch (Exception e) {
            System.out.println("�޸���ҵ������Ϣʱ��?" + e);
            return false;
        }
    }

    //��ϸ��ʾ�����¼
    public TradeClass show(String where) {
        try {
            String strSql = "select * from  " + strTableName + "  ".concat(where);
            ResultSet rs = db.executeQuery(strSql);
            if (rs != null && rs.next())
                return load(rs, true);
            else
                return null;
        } catch (Exception ee) {
            return null;
        }
    }

    //��¼��ת��Ϊ����
    public TradeClass load(ResultSet rs, boolean isView) {
        TradeClass theBean = new TradeClass();
        try {
            theBean.setStrId(rs.getString("strId"));
            theBean.setStrCode(rs.getString("strCode"));
            theBean.setStrName(rs.getString("strName"));
            theBean.setStrParentId(rs.getString("strParentId"));
            theBean.setStrCreator(rs.getString("strCreator"));
            theBean.setdCreatDate(rs.getString("dCreatDate"));
           theBean.setStrRemark(rs.getString("strRemark"));
           
        } catch (Exception e) {
            e.printStackTrace();
        }
        return theBean;
    }

    //��ѯ�������ļ�¼����
    public int getCount(String where) {
        int count = 0;
        try {
            String sql = "SELECT count(*) FROM " + strTableName + "  ";
            if (where.length() > 0) {
                where = where.toLowerCase();
                if (where.indexOf("order") > 0)
                    where = where.substring(0, where.lastIndexOf("order"));
                sql = String.valueOf(sql) + String.valueOf(where);
            }
            ResultSet rs = db.executeQuery(sql);
            if (rs.next())
                count = rs.getInt(1);
            rs.close();
            return count;
        } catch (Exception ee) {
            ee.printStackTrace();
            return count;
        }
    }

    //�б��¼��
    public Vector list(String where, int startRow, int rowCount) {
        Vector beans = new Vector();
        try {
            String sql = "SELECT *  FROM  " + strTableName;
            if (where.length() > 0)
                sql = String.valueOf(sql) + String.valueOf(where);
            Statement s = db.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            if (startRow != 0 && rowCount != 0)
                s.setMaxRows((startRow + rowCount) - 1);
            ResultSet rs = s.executeQuery(sql);
            if (rs != null && rs.next()) {
                if (startRow != 0 && rowCount != 0)
                    rs.absolute(startRow);
                do {
                	TradeClass theBean = new TradeClass();
                    theBean = load(rs, false);
                    beans.addElement(theBean);
                } while (rs.next());
            }
            rs.close();
            s.close();
        } catch (Exception ee) {
            ee.printStackTrace();
        }
        return beans;
    }

    private String strId;//	��λ��id��
    private String strCode;//	Ͻ�����
    private String strName;//	�������
    private String strParentId;//		 	�ϼ�Ͻ��ID
    private String strCreator;//			������
    private String dCreatDate;//		����ʱ��
    private String strRemark;	//��ע
    
    
    private int intLevel;	// �㼶��ʡ��1���У�2������3
    
    public static TradeClass rootTradeClass;	//����Ͻ��
    public static HashMap<String, TradeClass> hmTradeClass;	//����Ͻ���ϣ��
    public static Vector<TradeClass> vctTradeClassTree;	//����Ͻ������νṹ
    
    public static void init() {
    	
    	///	return;
    	
    	hmTradeClass = new HashMap<String, TradeClass>();
    	vctTradeClassTree = new Vector<TradeClass>();
    	String sql = "SELECT * FROM " + strTableName + " ORDER BY strCode";
    	try {
    		Connection con = DbConnect.getStaticCon();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
            	TradeClass theBean = new TradeClass();
            	theBean.setStrId(rs.getString("strId"));
                theBean.setStrCode(rs.getString("strCode"));
                theBean.setStrName(rs.getString("strName"));
                theBean.setStrParentId(rs.getString("strParentId"));
                theBean.setStrCreator(rs.getString("strCreator"));
                theBean.setdCreatDate(rs.getString("dCreatDate"));
              // theBean.setStrPoorFlag(rs.getString("strPoorFlag"));
                //theBean.setStrDistrictFlag(rs.getString("strDistrictFlag"));
                theBean.setStrRemark(rs.getString("strRemark"));
              //  theBean.setStrAllName(rs.getString("strAllName"));
                TradeClass parent = hmTradeClass.get(theBean.getStrParentId());
                if (parent != null) {
                	parent.addChild(theBean);
                	theBean.setIntLevel(parent.getIntLevel() + 1);
                } else {
                	theBean.setIntLevel(1);
                }
                hmTradeClass.put(theBean.getStrId(), theBean);
                vctTradeClassTree.add(theBean);
            }
            System.out.println("----------------��ʼ����ҵ����ɹ���");
            rs.close();
            stmt.close();
            con.close();
    	} catch (Exception e) {
    		e.printStackTrace();
    		System.out.println("----------------��ʼ����ҵ����ʧ�ܣ�����");
    	}
    }
    
    public static TradeClass getRootByName(String strName) {
    	TradeClass root = null;
    	for (int i = 0; i < vctTradeClassTree.size(); i++) {
    		if (strName.equals(vctTradeClassTree.get(i).getStrName())) {
    			root = vctTradeClassTree.get(i);
    			break;
    		}
    	}
    	if (root != null) {
    		while (root.getIntLevel() > 1) {
    			root = hmTradeClass.get(root.getStrParentId());
    		}
    	}
    	return root;
    }
    
   
	public String getStrId() {
        return strId;
    }

    public void setStrId(String strId) {
        this.strId = strId;
    }

    public void setStrCode(String strCode) {
		this.strCode = strCode;
	}

	public String getStrCode() {
		return strCode;
	}

	public void setStrName(String strName) {
		this.strName = strName;
	}

	public String getStrName() {
		return strName;
	}

	public String getStrParentId() {
        return Format.removeNull(strParentId);
    }

    public void setStrParentId(String strParentId) {
        this.strParentId = strParentId;
    }

    public String getStrCreator() {
        return strCreator;
    }

    public void setStrCreator(String strCreator) {
        this.strCreator = strCreator;
    }

    public String getdCreatDate() {
        return dCreatDate;
    }

    public void setdCreatDate(String dCreatDate) {
        this.dCreatDate = dCreatDate;
    }

	
    public void setStrRemark(String strRemark) {
		this.strRemark = strRemark;
	}

	public String getStrRemark() {
		return strRemark;
	}

	public void setIntLevel(int intLevel) {
		this.intLevel = intLevel;
	}

	public int getIntLevel() {
		return intLevel;
	}

	private Vector<TradeClass> children = new Vector<TradeClass>(); //�¼���

    public Vector<TradeClass> getChildren() {
        return children;
    }

    public void setChildren(Vector<TradeClass> children) {
        this.children = children;
    }

    public void addChild(TradeClass district) {
        this.children.add(district);
    }

    /**
     * �ж��Ƿ����¼��û���
     * @return
     */
    public boolean haveChild() {
        return this.children.size() > 0;
    }
    
    public void getChildLevel(Vector<TradeClass> vctChild, int intLevel) {
    	for (int i = 0; i < children.size(); i++) {
    		if (children.get(i).intLevel == intLevel)
    			vctChild.add(children.get(i));
    		else if (children.get(i).intLevel < intLevel)
    			children.get(i).getChildLevel(vctChild, intLevel);
    	}
    }

	public static String getIdByName(String strName) {
		for (int i = 0; i < vctTradeClassTree.size(); i++) {
			if (!strName.equals("")){
				if (vctTradeClassTree.get(i).getStrName().equals(strName))
					return vctTradeClassTree.get(i).getStrId();
				}
				else 
					return null;
			}
		return null;
	}
	public static String getNameById(String strId) {
		for (int i = 0; i < vctTradeClassTree.size(); i++) {
			if (vctTradeClassTree.get(i).getStrId().equals(strId))
				return vctTradeClassTree.get(i).getStrName();
		}
		return null;
	}
	public static TradeClass getTradeClassByName(String strName) {
		for (int i = 0; i < vctTradeClassTree.size(); i++) {
			if (vctTradeClassTree.get(i).getStrName().equals(strName))
				return vctTradeClassTree.get(i);
		}
		return null;
	}
	
	public Vector<TradeClass> getRoot() {
		Vector<TradeClass> strvct=new Vector();
		for (int i = 0; i < vctTradeClassTree.size(); i++) {
			if (vctTradeClassTree.get(i).getStrParentId().equals("0"))
			{
				strvct.add(vctTradeClassTree.get(i));
				
			}
				//return strvct;
		}
		return strvct;
	}

	
	
}
